<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/11
 * Time: 18:05
 */

namespace Admin\Model;

use Common\Common\MyPage;
use Think\Exception;
use Think\Log;
use Think\Model;
class RouteModel extends Model
{
    /**
     * 添加路线
     * @param $data 路线数据
     * @return 返回添加结果
     */
    public function addRoute($data){
        $Route = M('route');

        $tmp_name = I('post.image_name/s');

        $data['startTime'] = date('Y-m-d H:i:s',time());

        $data['endTime'] = date('Y-m-d H:i:s',time());

        $tmp_path = implode(DIRECTORY_SEPARATOR,
            array(__PUBLIC__,'uploads_tmp','route',
                $tmp_name));

        $tar_path = implode(DIRECTORY_SEPARATOR,
            array(__PUBLIC__,'uploads'));

        try {
            if (!file_exists($tar_path)) {
                mkdir($tar_path);
            }

            $tar_path = implode(DIRECTORY_SEPARATOR,
                array($tar_path, 'routeImage'));

            if (!file_exists($tar_path)) {
                mkdir($tar_path);
            }

            $tar_path = implode(DIRECTORY_SEPARATOR,
                array($tar_path, $tmp_name));

            rename($tmp_path, $tar_path);

            $data['img_url'] = $tmp_name;

        }catch (Exception $e){
            Log::write(json_encode($e->getMessage),'ERR');

            $data['img_url'] = '';
        }

        Log::write(json_encode($data),'WARN');

        return $Route->add($data);


    }

    public function getRoute($title='',$type=2,$status,$page_size=15){
        $data = new \stdClass();
        $now = date('y-m-d h:i:s',time());

         $route_model = M('route');

        $where = array(
            'name'     =>  array('lIKE','%'.$title.'%'),
            'status' => $status
        );

        if($type == 2){
            $where['endTime'] = array('LT',$now);
        }else if($type == 1){
            $where['endTime'] = array('GT',$now);
        }

        $count = $route_model->where($where)->count();

        $Page = new MyPage($count,$page_size);

        $data->show=$Page->showData();

        $dds = $route_model->where($where)->order('sort desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $data->data = $dds;

        return $data;

    }

    public function deleteRoute($ids){

        $where['id'] = array('in',$ids);

        $temps = $this->where($where)->select();

        foreach($temps as $key=>$temp){
            @unlink(__PUBLIC__ . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'routeImage' . DIRECTORY_SEPARATOR . $temp['img_url']);
        }

        $result = $this->where($where)->delete();

        if($result){
            return true;
        }else{
            return false;
        }
    }

    public function changeRoute($data){
        return $this->where($data['id'])->save($data);
    }

    public function getRouteById($id){

        $result = $this->where('id='.$id)->find();

        $temp = htmlspecialchars_decode($result['text']);

        $result['text'] = $temp;

        return $result;

    }

    public function update($data){

        $Activity = M('route');

        $tmp_name = $data['img_url'];

        if(intval($tmp_name) == -1){

            if(isset($data['img_url'])){
                unset($data['img_url']);
            }

        }else{

            if($tmp_name != '') {

                $tmp_path = __PUBLIC__ . DIRECTORY_SEPARATOR . 'uploads_tmp' . DIRECTORY_SEPARATOR . 'route' . DIRECTORY_SEPARATOR . $tmp_name;

                $tar_path = __PUBLIC__ . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'routeImage' . DIRECTORY_SEPARATOR . $tmp_name;

                @rename($tmp_path, $tar_path);

                $data['img_url'] = $tmp_name;

                $temp = $this->where(array('id'=>$data['id']))->find();

                if($temp['img_url'] != $data['img_url'])
                    @unlink(__PUBLIC__ . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'routeImage' . DIRECTORY_SEPARATOR . $temp['img_url']);


            }
            Log::write(json_encode($data),'WARN');
        }

        $id = $data['id'];

        $where['id'] = $id;

        unset($data['id']);

        unset($data['startTime']);

        $Activity->where($where)->setField($data);

        $inId = $id;

        return $inId;

    }


}